package com.boc.langxiang.ui.activity.index;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.inputmethod.EditorInfo;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.TextView;

import com.boc.langxiang.R;
import com.boc.langxiang.contract.index.ArticleDetailWordsContract;
import com.boc.langxiang.entity.bean.ShareBean;
import com.boc.langxiang.entity.events.ShareEvent;
import com.boc.langxiang.entity.response.ArticleEntity;
import com.boc.langxiang.entity.response.ArticleShareDetailContent;
import com.boc.langxiang.net.H5Url;
import com.boc.langxiang.presenter.index.ArticleDetailWordsPresenter;
import com.boc.langxiang.ui.activity.BaseToolBarActivity;
import com.boc.langxiang.ui.dialog.SharePadDlg;
import com.boc.langxiang.ui.view.webview.ObservableScrollViewCallbacks;
import com.boc.langxiang.ui.view.webview.ObservableWebView;
import com.boc.langxiang.ui.view.webview.ScrollState;
import com.boc.langxiang.util.KeyBoardUtil;
import com.boc.langxiang.util.L;
import com.boc.langxiang.util.T;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 热门文章详情
 */
public class ArticleDetailActivity extends BaseToolBarActivity implements ObservableScrollViewCallbacks,
        ArticleDetailWordsContract.View {

    @BindView(R.id.webView)
    ObservableWebView mWebView;
    @BindView(R.id.content)
    EditText mContent;
    private String webUrl;
    private String mId;
    private ArticleDetailWordsContract.Presenter mPresenter;

    private ArticleEntity mArticleEntity;
    private boolean mIsGuanZhu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_article_detail);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);

        Intent intent = getIntent();
        mArticleEntity = intent.getParcelableExtra("data");
        if (mArticleEntity == null) {
            finish();
            return;
        }
        mIsGuanZhu = intent.getBooleanExtra("isGuanZhu", false);
        if (mIsGuanZhu) {
            mId = mArticleEntity.getNews_id();
        } else {
            mId = mArticleEntity.getId();
        }

        initView();
        initWebViews();

        mPresenter = new ArticleDetailWordsPresenter(this);

    }

    private void initView() {
        mContent.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEND) {
                    KeyBoardUtil.closeKeybord(ArticleDetailActivity.this);
                    boolean login = isLogin();
                    if (login) {
                        mPresenter.articlefeedback(mId, mContent.getText().toString());
                        mContent.setText("");
                        return true;
                    }
                }
                return false;
            }
        });
    }

    private void initWebViews() {
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Log.d("url", url);
                if (url.equals(webUrl)) {
                    view.loadUrl(url);
                } else if (url.startsWith(webUrl + "/")) {
                    String index = "";
                    index = url.replace(webUrl + "/", "");
                    mWebView.loadUrl(webUrl + "/" + index);
                }
                return true;
            }
        });

        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setBuiltInZoomControls(false);
        webSettings.setUseWideViewPort(true);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setLoadsImagesAutomatically(true);
        mWebView.setScrollViewCallbacks(this);

        webUrl = H5Url.ARTICLE_DETAIL + mId;
        mWebView.loadUrl(webUrl);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_share_black, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_share) {
            L.i("share");
//            mPresenter.shareClick(mId);

            //-----
            ShareBean mShareBean = new ShareBean();
            mShareBean.setTitle(mArticleEntity.getTitle());
            mShareBean.setText(mArticleEntity.getContent());
            mShareBean.setTargetUrl(webUrl);
            mShareBean.setUmImage(new UMImage(mContext, R.drawable.ic_launcher));
            new SharePadDlg(mContext, SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.SINA, SHARE_MEDIA.QQ).setShareBean(mShareBean).show();

            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onScrollChanged(int scrollY, boolean firstScroll, boolean dragging) {
//        int baseColor = getResources().getColor(R.color.colorPrimary);
//        float alpha = Math.min(1, (float) scrollY / mParallaxImageHeight);
//        toolbar.setBackgroundColor(ScrollUtils.getColorWithAlpha(alpha, baseColor));
    }

    @Override
    public void onDownMotionEvent() {

    }

    @Override
    public void onUpOrCancelMotionEvent(ScrollState scrollState) {

    }

    @Override
    public void showLoading() {
        showProgressDialog("发表留言...");
    }

    @Override
    public void hideLoading() {
        hideDialog();
    }

    @Override
    public void onError(String code, String msg) {
        T.showShort(mContext, msg);
    }

    @Override
    public void articleFeedbackSuccess() {
        mWebView.reload();
    }

    @Override
    public void getShareDataSuccess(ArticleShareDetailContent.RowBean row) {
//        mShareBean = new ShareBean();
//        mShareBean.setTitle(row.getTitle());
//        mShareBean.setText(row.getContent());
//        mShareBean.setTargetUrl(webUrl);
//        mShareBean.setUmImage(new UMImage(mContext, R.drawable.ic_launcher));
//        new SharePadDlg(mContext, SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.SINA, SHARE_MEDIA.QQ).setShareBean(mShareBean).show();

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onShareEvent(ShareEvent event) {
        mPresenter.shareClick(mId);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode,resultCode,data);
    }
}
